package com.fjt.book_trade.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fasterxml.jackson.databind.jsontype.impl.StdSubtypeResolver;
import com.fjt.book_trade.pojo.User;
import com.fjt.book_trade.service.UserService;

@Controller
public class UserController {
	
	private UserService userService;

	public UserService getUserService() {
		return userService;
	}
	@Resource(name="userService")
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
    @RequestMapping("/user/register") 
    @ResponseBody
    public ModelAndView register(@RequestBody User user ){
    	Map<String,String> modelMap=new HashMap<String,String>();
//      modelMap=userService.validateUser(username, password, request);  
    	modelMap=userService.addUser(user);
    	ModelAndView modelView=new ModelAndView();  
    	modelView.addAllObjects(modelMap);  
    	System.out.println("/user/register");
    	return modelView;
    }
    
    @RequestMapping("/user/login") 
    @ResponseBody
    public ModelAndView login(@RequestBody User user,
    		HttpServletRequest request){  
    	Map<String,String> modelMap=new HashMap<String,String>();
//      modelMap=userService.validateUser(username, password, request);  
    	modelMap=userService.validateUser(user, request);
    	modelMap.put("JSESSIONID", request.getSession().getId());
    	ModelAndView modelView=new ModelAndView();  
    	modelView.addAllObjects(modelMap);  
    	System.out.println(user.getUsername());
    	System.out.println(user.getPassword());
    	System.out.println("/user/login");
    	return modelView;
    }
    
    @RequestMapping("/user/userinfo") 
    @ResponseBody
    public ModelAndView getUserInfo(HttpServletRequest request){
    	Map<String,Object> modelMap=new HashMap<String,Object>();
    	ModelAndView modelView=new ModelAndView(); 
//      modelMap=userService.validateUser(username, password, request);  
    	HttpSession session=request.getSession();
    	System.out.println(session.getId());
    	String username=(String)session.getAttribute("username");
    	if(username!=null){
    		modelMap=userService.getUserInfo(username);
	    	modelView.addAllObjects(modelMap); 
	    	System.out.println("/user/userinfo");
	    	return modelView;
    	}else{
    		modelMap.put("result", "fail");
    		modelView.addAllObjects(modelMap);
    		System.out.println("/user/userinfo no session");
    		return modelView;
    	}
    	
    }
    
    @RequestMapping("/user/modifypassword") 
    @ResponseBody
    public ModelAndView modifypassword(@RequestBody User user,HttpServletRequest request ){  
    	Map<String,String> modelMap=new HashMap<String,String>();
    	ModelAndView modelView=new ModelAndView();
//      modelMap=userService.validateUser(username, password, request);
    	HttpSession session=request.getSession();
    	String username=(String)session.getAttribute("username");
    	if(username!=null){
    		user.setUsername(username);
    	}else{
    		modelMap.put("result", "fail");
    		modelView.addAllObjects(modelMap);
    		System.out.println("/user/modifypassword no session");
    		return modelView;
    	}
    	modelMap=userService.modifyPassWord(user);
    	System.out.println("/user/modifypassword");
    	modelView.addAllObjects(modelMap);  
    	return modelView;
    }
    
    @RequestMapping("/user/modifyuserinfo") 
    @ResponseBody
    public ModelAndView modifyuserinfo(@RequestBody User user,HttpServletRequest request ){
    	Map<String,String> modelMap=new HashMap<String,String>();
    	ModelAndView modelView=new ModelAndView(); 
//      modelMap=userService.validateUser(username, password, request); 
    	HttpSession session=request.getSession();
    	String username=(String)session.getAttribute("username");
    	if(username!=null){
    		user.setUsername(username);
    		System.out.println(user.getNewpwd());

    		modelMap=userService.modifyUserInfo(user);
    		if(user.getNewpwd()!=""){
    			modelMap.putAll(userService.modifyPassWord(user));
    		}
        	modelView.addAllObjects(modelMap); 
        	modelView.addAllObjects(userService.getUserInfo(user.getUsername()));
        	System.out.println("/user/modifyuserinfo");
        	return modelView;
    	}else{
    		modelMap.put("result", "fail");
    		modelView.addAllObjects(modelMap);
    		System.out.println("/user/modifyuserinfo no session");
    		return modelView;
    	}  	
    }
    
    @RequestMapping("/user/logout") 
    @ResponseBody
    public ModelAndView logout(
    		HttpServletRequest request){  
    	Map<String,Object> modelMap=new HashMap<String,Object>();
    	HttpSession session=request.getSession(false);
    	if(session!=null){
    		session.removeAttribute("username");
    		session.invalidate();
    	}	
    	ModelAndView modelView=new ModelAndView();
    	modelMap.put("result", "注销成功");
    	modelView.addAllObjects(modelMap); 
    	System.out.println("/p_user/logout");
    	return modelView;
    	
    }
}
